package com.ruoyi;

import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.ruoyi.common.utils.baidu.ConnUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class BaiduTtsTest2 {

    private String tempToken = "25.79b850086071ba7ed3878a9cd95da1c0.315360000.2013470715.282335-41603746";

    private final String text = "A01电量低，请及时充电";
    // 发音人选择, 基础音库：0为度小美，1为度小宇，3为度逍遥，4为度丫丫，
    // 精品音库：5为度小娇，103为度米朵，106为度博文，110为度小童，111为度小萌，默认为度小美
    private final int per = 0;
    // 语速，取值0-15，默认为5中语速
    private final int spd = 10;
    // 音调，取值0-15，默认为5中语调
    private final int pit = 5;
    // 音量，取值0-9，默认为5中音量
    private final int vol = 5;
    // 下载的文件格式, 3：mp3(default) 4： pcm-16k 5： pcm-8k 6. wav
    private final int aue = 3;

    public static void main(String[] args) {
        (new BaiduTtsTest2()).run();

//        String decode1 = URLUtil.decode("A01%25E7%2594%25B5%25E9%2587%258F%25E4%25BD%258E%25EF%25BC%258C%25E8%25AF%25B7%25E5%258F%258A%25E6%2597%25B6%25E5%2585%2585%25E7%2594%25B5");
//        String decode2 = URLUtil.decode(decode1);
//        log.info("decode1: {}, decode2: {}", decode1, decode2); // "A01电量低，请及时充电"
    }

    private void run() {
        HttpRequest httpRequest = HttpUtil.createPost("https://tsn.baidu.com/text2audio");
        Map<String, Object> paramsMap = new HashMap<>();
//        paramsMap.put("tex", URLUtil.encode(requestBody.getText()));
        paramsMap.put("tex", ConnUtil.urlEncode(ConnUtil.urlEncode(text)));
        paramsMap.put("tok", tempToken);
        paramsMap.put("cuid", "1234567zhangjianwei");
        paramsMap.put("ctp", "1"); //必填，客户端类型选择，web端填写固定值1
        paramsMap.put("lan", "zh");
        paramsMap.put("per", per);
        paramsMap.put("spd", spd);
        paramsMap.put("pit", pit);
        paramsMap.put("vol", vol);
        paramsMap.put("aue", aue);
        log.info("text2audio paramsMap: {}", paramsMap);
//        httpRequest.form(paramsMap);
        UrlBuilder urlBuilder = UrlBuilder.of("https://tsn.baidu.com/text2audio");
        urlBuilder.addQuery("tex", ConnUtil.urlEncode(ConnUtil.urlEncode(text)));
        urlBuilder.addQuery("tok", tempToken);
        urlBuilder.addQuery("cuid", "1234567zhangjianwei");
        urlBuilder.addQuery("ctp", "1"); //必填，客户端类型选择，web端填写固定值1
        urlBuilder.addQuery("lan", "zh");
        urlBuilder.addQuery("per", per);
        urlBuilder.addQuery("spd", spd);
        urlBuilder.addQuery("pit", pit);
        urlBuilder.addQuery("vol", vol);
        urlBuilder.addQuery("aue", aue);
        log.info("text2audio urlBuilder: {}", urlBuilder);
        httpRequest.setUrl(urlBuilder);
        log.info("httpRequest: {}", httpRequest);
        HttpResponse httpResponse = httpRequest.execute();
        log.info("httpResponse: {}", httpResponse);

//        Map<String, List<String>> headers = httpResponse.headers();
//        for (String key : headers.keySet()) {
//            log.info("key: {}----value: {}", key, headers.get(key));
//        }

        String contentType = httpResponse.header("Content-Type");
        log.info("contentType: {}", contentType);
        if (contentType.contains("audio/")) {
            String format = getFormat(aue);
            log.info("format: {}", format);
            File file = new File("result2." + format); // 打开mp3文件即可播放
            httpResponse.writeBodyForFile(file, new StreamProgress() {
                @Override
                public void start() {
                    log.info("开始拷贝.");
                }

                @Override
                public void progress(long total, long progressSize) {
                    log.info("拷贝中..., 文件总大小: {},当前已拷贝大小：{}", total, progressSize);
                }

                @Override
                public void finish() {
                    log.info("拷贝完成.");
                    log.info("file路径：{}", file.getAbsoluteFile());
                }
            });

        }
    }

    // 下载的文件格式, 3：mp3(default) 4： pcm-16k 5： pcm-8k 6. wav
    private String getFormat(int aue) {
        String[] formats = {"mp3", "pcm", "pcm", "wav"};
        return formats[aue - 3];
    }

}
